﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AceMobile.Models;
using AceMobile.Models.Accessories;
using AceMobile.Models.Phones;
using AceMobile.Models.Tablets;

namespace AceMobile.Controllers
{
    public class CartController : Controller
    {
        MobileWebContext db = new MobileWebContext();
        //
        // GET: /Cart/        
        public ActionResult Index()
        {
            if (Session["cart"] == null)
            {
                Session["cart"] = "";
            }
            Cart cart = new Cart(Session["cart"].ToString());            
            if (cart.Orders.Count > 0)
            {
                foreach (var item in cart.Orders)
                {
                    
                    if (item.Type == "phone")
                    {
                        Phone product = db.Phones.Find(item.ID);
                        item.Name = product.Name;
                        item.Price = product.Price;
                    }
                    if (item.Type == "accessory")
                    {
                        Accessory product = db.Accessories.Find(item.ID);
                        item.Name = product.Name;
                        item.Price = product.Price;
                    }
                    if (item.Type == "tablet")
                    {
                        Tablet product = db.Tablets.Find(item.ID);
                        item.Name = product.Name;
                        item.Price = product.Price;
                    }                                        
                }
            }
            ViewBag.priceAll = cart.getPrice();
            return View(cart.Orders.ToList());
        }
        [HttpPost]
        public ActionResult Add2Cart(int ID, string Type)
        {
            if (Session["cart"] == null)
            {
                Session["cart"] = "";
            }            
            Cart cart = new Cart(Session["cart"].ToString());
            bool test = false;
            if (cart.Orders.Count != 0)
            {
                foreach (var item in cart.Orders)
                {
                    if ((item.ID == ID) && (item.Type == Type))
                    {
                        item.Amount++;
                        test = true;
                        break;
                    }
                }
            }
            // neu khong co order nao truoc thi them vao order moi
            if (test == false)
            {
                cart.addOrder(ID, Type);
            }
            Session["cart"] = cart.tostring();
            Session["Amount"] = cart.getCount().ToString();
            Session["Price"] = cart.getPrice().ToString();
            return Json(new { Amount = Session["Amount"].ToString(),
                              Price = String.Format("{0:#,#}", Convert.ToInt32(Session["Price"].ToString()))});
        }
        [HttpPost]
        public ActionResult ChangeAmount(int ID, string type, int Amount)           
        {                         
            if (Amount < 0 )
            {
                Amount = 0;
            }
            if (Session["cart"] == null)
            {
                Session["cart"] = "";
            }
            Cart cart = new Cart(Session["cart"].ToString());
            if (cart.Orders.Count > 0)
            {
                foreach (var item in cart.Orders)
                {
                    if (type=="phone")
                    {
                        Phone product = db.Phones.Find(item.ID);
                        item.Price = product.Price;
                    }
                    if (type == "accessory")
                    {
                        Accessory product = db.Accessories.Find(item.ID);
                        item.Price = product.Price;
                    }
                    if (type == "tablet")
                    {
                        Tablet product = db.Tablets.Find(item.ID);
                        item.Price = product.Price;
                    }
                    
                    if (item.ID == ID)
                    {
                        item.Amount = Amount;
                        break;
                    }                                        
                }                
            }
            Session["cart"] = cart.tostring();
            Session["Amount"] = cart.getCount().ToString();
            Session["Price"] = cart.getPrice().ToString();
            return Json(new { priceall = String.Format("{0:#,#}", Convert.ToInt32(Session["Price"].ToString())), Amount = Session["Amount"].ToString() });
        }
        [HttpPost]
        public ActionResult Delete(int id, string type)
        {
            if (Session["cart"] == null)
            {
                Session["cart"] = "";
            }
            Cart cart = new Cart(Session["cart"].ToString());            
            if (cart.Orders.Count > 0)
            {
                foreach (var item in cart.Orders)
                {
                    if ((item.ID == id) && (item.Type == type))
                    {
                        cart.Orders.Remove(item);
                        break;
                    }
                }                
            }
            Session["cart"] = cart.tostring();
            Session["Amount"] = cart.getCount().ToString();
            Session["Price"] = cart.getPrice().ToString();
            return Json(new { priceall = String.Format("{0:#,#}", Convert.ToInt32(Session["Price"].ToString())), Amount = Session["Amount"].ToString() });
        }
        public ActionResult Order()
        {
            
            try
            {
                string login = Session["Login"].ToString();
                var user = db.Accounts.Where(a => a.UserName == login).First();
                if (user != null)
                {
                    ViewBag.User = user;
                }
                else
                {
                    return RedirectToAction("LogOn", "Account");
                }
            }
            catch (Exception)
            {
                return RedirectToAction("LogOn", "Account");
            }            
            if (Session["cart"] == null)
            {
                Session["cart"] = "";
            }
            Cart cart = new Cart(Session["cart"].ToString());
            if (cart.Orders.Count > 0)
            {
                foreach (var item in cart.Orders)
                {

                    if (item.Type == "phone")
                    {
                        Phone product = db.Phones.Find(item.ID);
                        item.Name = product.Name;
                        item.Price = product.Price;
                    }
                    if (item.Type == "accessory")
                    {
                        Accessory product = db.Accessories.Find(item.ID);
                        item.Name = product.Name;
                        item.Price = product.Price;
                    }
                    if (item.Type == "tablet")
                    {
                        Tablet product = db.Tablets.Find(item.ID);
                        item.Name = product.Name;
                        item.Price = product.Price;
                    }
                }
            }
           
            ViewBag.priceAll = cart.getPrice();
            return View(cart.Orders.ToList());
        }
        public ActionResult OrderConfirm()
        {
            try
            {            
                Cart cart = new Cart(Session["cart"].ToString());
                if (cart.Orders.Count <= 0)
                {
                    Session["mess"] = "Bạn chưa đặt hàng!";
                    return RedirectToAction("Order");
                }
                string login = Session["Login"].ToString();
                var user = db.Accounts.Where(a => a.UserName == login).First();
                if (user != null)
                {
                    ViewBag.User = user;
                    foreach (var item in cart.Orders)
                    {
                        OrderList or = new OrderList() { 
                                ProductID = item.ID,
                                ProductType = item.Type,
                                UserID = user.ID, 
                                Amount = item.Amount,
                                Status = 0 
                                /*
                                 * 0: chưa thanh toán
                                 * 1: đang chuyển hàng
                                 * 2: đã thanh toán
                                 */
                            };
                        db.OrderLists.Add(or);
                    }
                    db.SaveChanges();
                }
                else
                {
                    return RedirectToAction("LogOn", "Account");
                }
            }
            catch (Exception)
            {
                return RedirectToAction("LogOn", "Account");
            }
            Session["cart"] = "";
            Session["Amount"] = 0;
            Session["Price"] = 0;
            return View();
        }        
    }
}
